package com.zjh.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.imageaudit20191230.models.*;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.teautil.Common;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.InputStream;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SpringBootTest(classes = Aliyuntexttest.class)
@RunWith(SpringRunner.class)
public class Aliyuntexttest {
    public static com.aliyun.imageaudit20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        /*
          初始化配置对象com.aliyun.teaopenapi.models.Config
          Config对象存放 AccessKeyId、AccessKeySecret、endpoint等配置
         */
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
        return new com.aliyun.imageaudit20191230.Client(config);
    }
    @Test
    public void theone () throws Exception {
        // 创建AccessKey ID和AccessKey Secret，请参考https://help.aliyun.com/document_detail/175144.html。
        // 如果您使用的是RAM用户的AccessKey，还需要为RAM用户授予权限AliyunVIAPIFullAccess，请参考https://help.aliyun.com/document_detail/145025.html。
        // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
        String accessKeyId = "LTAI5tDJDzuzL3hCW8H6mQWd";
        String accessKeySecret = "UQ9PfoW2nLVBESJu3RH01Fwb4EcGvV";
        com.aliyun.imageaudit20191230.Client client = Aliyuntexttest.createClient(accessKeyId, accessKeySecret);
        ScanTextRequest.ScanTextRequestTasks tasks = new ScanTextRequest.ScanTextRequestTasks()
                .setContent("习近平sifjsdilfjsd");
        ScanTextRequest.ScanTextRequestLabels labels = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("ad");
        ScanTextRequest.ScanTextRequestLabels labels1 = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("spam");
        ScanTextRequest.ScanTextRequestLabels labels2 = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("politics");
        ScanTextRequest.ScanTextRequestLabels labels3 = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("abuse");
        ScanTextRequest.ScanTextRequestLabels labels4 = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("terrorism");
        ScanTextRequest.ScanTextRequestLabels labels5 = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("porn");
        ScanTextRequest.ScanTextRequestLabels labels6 = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("flood");
        ScanTextRequest.ScanTextRequestLabels labels7 = new ScanTextRequest.ScanTextRequestLabels()
                .setLabel("contraband");
        com.aliyun.imageaudit20191230.models.ScanTextRequest scanTextRequest = new com.aliyun.imageaudit20191230.models.ScanTextRequest()
                .setLabels(java.util.Arrays.asList(
                        labels,
                        labels1,
                        labels2,
                        labels3,
                        labels4,
                        labels5,
                        labels6,
                        labels7
                ))
                .setTasks(java.util.Arrays.asList(
                        tasks
                ));
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印API的返回值
            ScanTextResponse response = client.scanTextWithOptions(scanTextRequest, runtime);
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response.body.getData())));
            String s = Common.toJSONString(TeaModel.buildMap(response.body.getData()));
            System.out.println(s);
            JSONObject jsonObject = JSON.parseObject(s);
            Object elements = jsonObject.get("Elements");
            String s1 = elements.toString();
            JSONArray objects = JSONArray.parseArray(s1);
            JSONObject jsonObject1 = objects.getJSONObject(0);
            JSONArray results = jsonObject1.getJSONArray("Results");
            JSONObject resultObj = results.getJSONObject(0);
            String suggestion = resultObj.getString("Suggestion");
            System.out.println(suggestion);

        } catch (TeaException error) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(error));
            // 获取单个字段
            System.out.println(error.getCode());
        }
    }


    //图片检测

//    public static com.aliyun.teaopenapi.models.Params createApiInfo() throws Exception {
//        com.aliyun.teaopenapi.models.Params params = new com.aliyun.teaopenapi.models.Params()
//                // 接口名称
//                .setAction("ScanImage")
//                // 接口版本
//                .setVersion("2019-12-30")
//                // 接口协议
//                .setProtocol("HTTPS")
//                // 接口 HTTP 方法
//                .setMethod("POST")
//                .setAuthType("AK")
//                .setStyle("RPC")
//                // 接口 PATH
//                .setPathname("/")
//                // 接口请求体内容格式
//                .setReqBodyType("formData")
//                // 接口响应体内容格式
//                .setBodyType("json");
//        return params;
//    }
    @Test
    public void testimage() throws Exception {
        String accessKeyId = "LTAI5tDJDzuzL3hCW8H6mQWd";
        String accessKeySecret = "UQ9PfoW2nLVBESJu3RH01Fwb4EcGvV";
        com.aliyun.imageaudit20191230.Client client = Aliyuntexttest.createClient(accessKeyId, accessKeySecret);
        // 场景二，使用任意可访问的url
        URL url = new URL("http://192.168.200.130:9000/leadnews/2025/08/15/02e6a1b52ab7494d84703047712afd45.png");
        InputStream inputStream1 = url.openConnection().getInputStream();
        ScanImageAdvanceRequest.ScanImageAdvanceRequestTask task0 = new ScanImageAdvanceRequest.ScanImageAdvanceRequestTask();
//        task0.setDataId("uuid-xxxx-xxxx-123456");
//        task0.setImageTimeMillisecond(1L);
//        task0.setInterval(1);
//        task0.setMaxFrames(1);
        task0.setImageURLObject(inputStream1);
        List<ScanImageAdvanceRequest.ScanImageAdvanceRequestTask> taskList = new ArrayList<>();
        taskList.add(task0);
        List<String> sceneList = new ArrayList<>();
        sceneList.add("porn");
        com.aliyun.imageaudit20191230.models.ScanImageAdvanceRequest scanImageAdvanceRequest = new com.aliyun.imageaudit20191230.models.ScanImageAdvanceRequest()
                .setTask(taskList)
                .setScene(sceneList);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            ScanImageResponse scanImageResponse = client.scanImageAdvance(scanImageAdvanceRequest, runtime);
            // 获取整体结果
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(scanImageResponse)));
            // 获取单个字段
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(scanImageResponse.body)));
            String jsonStr = Common.toJSONString(TeaModel.buildMap(scanImageResponse.body));
            // 解析为JSONObject
            JSONObject jsonObject = JSON.parseObject(jsonStr);

            // 逐层获取嵌套的JSON结构
            JSONObject data = jsonObject.getJSONObject("Data");
            JSONArray results = data.getJSONArray("Results");
            JSONObject firstResult = results.getJSONObject(0);
            JSONArray subResults = firstResult.getJSONArray("SubResults");
            JSONObject firstSubResult = subResults.getJSONObject(0);

            // 提取suggestion的值
            String suggestion = firstSubResult.getString("Suggestion");

            System.out.println("Suggestion: " + suggestion); // 输出：Suggestion: pass
        } catch (com.aliyun.tea.TeaException teaException) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段
            System.out.println(teaException.getCode());
        }
    }
}
